package com.inepex.graphicstests.client.govscanvasperf;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.inepex.graphicstests.client.misc.Globals;

public class Main extends FlowPanel {

	private static Runner runnerInstance;
	public static Runner getRunner(){
		if(runnerInstance == null){
			runnerInstance = new Runner();
		}
		return runnerInstance;
	}
	
	private Button stopAnimationButton;
	private Button startSimpleButton;
	private Button startIntelligentButton;
	private Button startDirectIneCanvasButton;
	private CheckBox showUpdateInfoCheckBox;
	private TextBox goCTextBox;
	private Label goCLabel;
	
	private Widget actualContent;
	
	public Main() {
		init();
	}
	
	private void init(){
		stopAnimationButton = new Button("stop animation");
		stopAnimationButton.addClickHandler(new ClickHandler() {	
			@Override
			public void onClick(ClickEvent event) {
				getRunner().stop();	
				stopAnimationButton.setEnabled(false);
				startDirectIneCanvasButton.setEnabled(true);
				startIntelligentButton.setEnabled(true);
				startSimpleButton.setEnabled(true);
			}
		});
		stopAnimationButton.setEnabled(false);
		
		startSimpleButton = new Button("new GOs");
		startSimpleButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Log.debug("Simple Usage started");
				setContent(new GraphicsSimpleUsage());
			}
			
		});
		
		startIntelligentButton = new Button("reuse GOs");
		startIntelligentButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Log.debug("Intelligent Usage started");
				setContent(new GraphicsIntelligentUsage());
			}
			
		});
		
		startDirectIneCanvasButton = new Button("direct IneCanvas (no GOs)");
		startDirectIneCanvasButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Log.debug("Direct IneCanvas usage started");
				setContent(new IneCanvasDirectUsage());
			}
		});
		
		showUpdateInfoCheckBox = new CheckBox("show update info");
		showUpdateInfoCheckBox.setValue(Globals.showUpdateInfo);
		showUpdateInfoCheckBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				Globals.showUpdateInfo = event.getValue();
			}
		});
		
		goCLabel = new Label("GO count:");
		goCTextBox = new TextBox();
		KeyPressHandler validator = new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				char keyCode = event.getCharCode();	
				if (!Character.isDigit(keyCode) && (keyCode != 127 || keyCode != 8)){
					((TextBox)event.getSource()).cancelKey();
				}
			}
		};
		ClickHandler clickClear = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				((TextBox)event.getSource()).setText("");				
			}
		};
		goCTextBox.addClickHandler(clickClear);
		goCTextBox.addKeyPressHandler(validator);
		
		HorizontalPanel hp = new HorizontalPanel();
		HorizontalPanel hp2 = new HorizontalPanel();
		
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		
		hp.add(goCLabel);
		hp.add(goCTextBox);
		hp.add(showUpdateInfoCheckBox);		
		hp2.add(startSimpleButton);
		hp2.add(startIntelligentButton);
		hp2.add(startDirectIneCanvasButton);
		hp2.add(stopAnimationButton);
		this.add(hp);
		this.add(hp2);
	}
	
	private void setContent(Widget content){
		if(actualContent != null){
			this.remove(actualContent);
		}
		getRunner().stop();
		int i;
		try{
			i = Integer.parseInt(goCTextBox.getText());
			if(i > 0){
				Globals.goCount = i;
			}
		}
		catch (Exception e) {
		}
		
		actualContent = content;
		this.add(actualContent);
		stopAnimationButton.setEnabled(true);
		startDirectIneCanvasButton.setEnabled(false);
		startIntelligentButton.setEnabled(false);
		startSimpleButton.setEnabled(false);
	}
	
}
